/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets.gp.patient;

import be.ugent.medirem.logic.PatientRemote;
import be.ugent.medirem.persistence.entity.details.PatientDetails;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Simon
 */
public class SearchPatientServlet extends HttpServlet {
    @EJB
    private PatientRemote patientBean;
   
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String ssn = request.getParameter("ssn");
        if (ssn == null || ssn.isEmpty()) {
            response.sendError(response.SC_BAD_REQUEST);
            return;
        }
        PatientDetails patient = patientBean.searchPatientsBySocialSecurityNumber(ssn);
        boolean success = patient != null;
        response.setContentType("text/x-json");
        PrintWriter out = response.getWriter();
        try {
            out.print("{success : " + success);
            if (success) {
                out.print(","+ patient.toJSON());
            }
            out.print("}");
        } finally {
            out.close();
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
